﻿using CSBackup_Web.Models;
using System;
using System.Web.Mvc;
using System.Web.Security;

namespace CSBackup_Web.Controllers
{
    public class AccountController : Controller
    {
        [AllowAnonymous]
        [HttpGet]
        public ActionResult LogIn()
        {
            return View(new LogInModel());
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult LogIn(LogInModel m, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(m.userName, m.password))
                {
                    FormsAuthentication.SetAuthCookie(m.userName, false);
                    return Redirect(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("password", "用户名或密码错误，请重新输入。");
                    return View(m);
                }
            }
            // If we got this far, something failed; redisplay form
            return View(m);
        }

        [Authorize]
        [HttpGet]
        public ActionResult AlterPw()
        {
            return View(new AlterPwModel());
        }

        [Authorize]
        [HttpPost]
        public ActionResult AlterPw(AlterPwModel m)
        {
            if (ModelState.IsValid)
            {
                // ChangePassword will throw an exception rather than return false in certain faiure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true);
                    changePasswordSucceeded = currentUser.ChangePassword(m.oldPassword, m.newPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }
                if (changePasswordSucceeded)
                {
                    TempData["alert"] = "密码修改成功，请使用新密码登录系统！";
                    return RedirectToAction("Login");
                }
                else
                {
                    ModelState.AddModelError("", "旧密码错误或新密码无效");
                }
            }

            // If we got this far, something failed; redisplay form
            return View(m);
        }

        [AllowAnonymous]
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Register(RegisterModel m)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(m.UserID, m.Password, null, null, null, true, null, out createStatus);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(m.UserID, createPersistentCookie: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    TempData["alert"] = "注册失败";
                }
            }

            // If we got this far, something failed; redisplay form
            return View(m);
        }
    }
}
